#ifndef __NETLIB_H__
#define __NETLIB_H__

#include "ostype.h"

#define NETLIB_OPT_SET_CALLBACK 1
#define NETLIB_OPT_SET_CALLBACK_DATA 2
#define NETLIB_OPT_GET_REMOTE_IP 3
#define NETLIB_OPT_GET_REMOTE_PORT 4
#define NETLIB_OPT_GET_LOCAL_IP 5
#define NETLIB_OPT_GET_LOCAL_PORT 6
#define NETLIB_OPT_SET_SEND_BUF_SIZE 7
#define NETLIB_OPT_SET_RECV_BUF_SIZE 8

#define NETLIB_MAX_SOCKET_BUF_SIZE (128 * 1024)

#ifdef __cplusplus
extern "C"
{
#endif

	extern int netlib_init();

	extern int netlib_destroy();

	extern int netlib_listen(
		const char *server_ip,
		uint16_t port,
		callback_t callback,
		void *callback_data);

	extern net_handle_t netlib_connect(
		const char *server_ip,
		uint16_t port,
		callback_t callback,
		void *callback_data);

	extern int netlib_send(net_handle_t handle, void *buf, int len);

	/// @brief recevice data from socket fd
	/// @param handle Socket fd
	/// @param buf started address
	/// @param len Length of Written bytes.
	/// @return the number read or -1 for errors.
	extern int netlib_recv(net_handle_t handle, void *buf, int len);

	extern int netlib_close(net_handle_t handle);

	/// @brief It is used to get/set Socket Object.
	/// The socket reference count is minus one.
	/// @param handle Socket fd
	/// @param opt 
	/// @param optval 
	/// @return Error Type.
	extern int netlib_option(net_handle_t handle, int opt, void *optval);

	extern int netlib_register_timer(callback_t callback, void *user_data, uint64_t interval);

	extern int netlib_delete_timer(callback_t callback, void *user_data);

	extern int netlib_add_loop(callback_t callback, void *user_data);

	extern void netlib_eventloop(uint32_t wait_timeout = 100);

	extern void netlib_stop_event();

	extern bool netlib_is_running();

#ifdef __cplusplus
}
#endif

#endif
